/*
 * Test
 */

import Test from 'constants/test';
import { CALL_BOOK_API } from '../middlewares';

/**
 * 创建测试
 * @param data TestModal
 */
export function createTest(lessonId: number, levelId: number, data: Record<string, any>) {
  data = { ...data, levelId };
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Test.CREATE, Test.CREATE_SUCCESS, Test.CREATE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/tests`,
      method: 'POST',
      body: data,
    },
  };
}

/**
 * 修改测试内容
 */
export function updateTest(lessonId: number, testId: number, data: Record<string, any>) {
  return {
    testId,
    [CALL_BOOK_API]: {
      types: [Test.UPDATE, Test.UPDATE_SUCCESS, Test.UPDATE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/tests/${testId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * 删除测试及相关数据
 * @param levelId: 课时ID
 * @param testId: 测试ID
 */
export function removeTest(lessonId: number, testId: number) {
  return {
    testId,
    [CALL_BOOK_API]: {
      types: [Test.REMOVE, Test.REMOVE_SUCCESS, Test.REMOVE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/tests/${testId}`,
      method: 'DELETE',
    },
  };
}

/**
 * 查询测试详情
 * @param data TestModal
 */
export function fetchTestDetail(lessonId: number, testId: number) {
  return {
    testId,
    [CALL_BOOK_API]: {
      types: [Test.FETCH, Test.FETCH_SUCCESS, Test.FETCH_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/tests/${testId}`,
      method: 'GET',
    },
  };
}

/**
 * 查询Test列表
 * @param lessonId 课时ID
 * @param levelId 关卡ID
 */
export function fetchTestList(lessonId: number, levelId: number) {
  return {
    levelId,
    [CALL_BOOK_API]: {
      types: [Test.FETCH_LIST, Test.FETCH_LIST_SUCCESS, Test.FETCH_LIST_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/tests`,
      method: 'GET',
      body: {
        levelId,
        page: 1,
        pageSize: 99,
      },
    },
  };
}

/**
 * 创建测试模板记录
 * @param data TplModal
 */
export function createTestTpl(data: Record<string, any>) {
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Test.CREATE_TPL, Test.CREATE_TPL_SUCCESS, Test.CREATE_TPL_FAIL],
      uri: '/course-api/a/test-templates',
      method: 'POST',
      body: data,
    },
  };
}

/**
 * 更新模板记录
 * @param data TestModal
 */
export function updateTestTpl(tplId: number, data: Record<string, any>) {
  return {
    tplId,
    [CALL_BOOK_API]: {
      types: [Test.UPDATE_TPL, Test.UPDATE_TPL_SUCCESS, Test.UPDATE_TPL_FAIL],
      uri: `/course-api/a/test-templates/${tplId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * 查询Test模板列表
 * @param filters Test查询过滤
 */
export function fetchTestTplList(filters: Record<string, any> = {}) {
  return {
    filters,
    [CALL_BOOK_API]: {
      types: [Test.FETCH_TPL_LIST, Test.FETCH_TPL_LIST_SUCCESS, Test.FETCH_TPL_LIST_FAIL],
      uri: '/course-api/a/test-templates',
      method: 'GET',
      body: filters,
    },
  };
}
